import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { SysAreaService } from './sys-area.service';
import { RepeatSubmit } from 'src/common/decorators/repeat-submit.decorator';
import { RequiresPermissions } from 'src/common/decorators/requires-permissions.decorator';
import { CreateMessagePipe } from 'src/common/pipes/createmessage.pipe';
import { BusinessTypeEnum, Log } from 'src/common/decorators/log.decorator';
import { DataObj } from 'src/common/class/data-obj.class';
import { UpdateMessagePipe } from 'src/common/pipes/updatemessage.pipe';
import {
  AddSysAreaDto,
  GetSysAreaListDto,
  UpdateSysAreaDto,
} from './dto/req-sys-area.dto';
import { User, UserEnum } from 'src/common/decorators/user.decorator';
import { DataScope } from 'src/common/type/data-scope.type';
import { Public } from 'src/common/decorators/public.decorator';

@Controller('system/area')
export class SysAreaController {
  constructor(private readonly sysAreaService: SysAreaService) {}
  
  /* 获取所有启用的地区列表（小程序端使用） */
  @Get('enabled')
  @Public()
  async enabledList() {
    const areaList = await this.sysAreaService.enabledList();
    return DataObj.create(areaList);
  }
  
  /* 新增 */
  @Post()
  @RepeatSubmit()
  @Log({
    title: '地区管理',
    businessType: BusinessTypeEnum.insert,
  })
  @RequiresPermissions('system:area:add')
  async add(@Body(CreateMessagePipe) addSysAreaDto: AddSysAreaDto) {
    await this.sysAreaService.add(addSysAreaDto);
  }

  /* 列表查询 */
  @Get('list')
  @RequiresPermissions('system:area:query')
  async list(
    @Query() getSysAreaListDto: GetSysAreaListDto,
    @User(UserEnum.dataScope) dataScope: DataScope,
  ) {
    return await this.sysAreaService.list(getSysAreaListDto, dataScope);
  }

  /* 查询出去这个id及其子地区外的其他地区 */
  @Get('list/exclude/:areaId')
  @RequiresPermissions('system:area:edit')
  async listExclude(@Param('areaId') areaId: number) {
    return await this.sysAreaService.listExclude(areaId);
  }

  /* 通过id查询 */
  @Get(':areaId')
  @RequiresPermissions('system:area:query')
  async oneByAreaId(@Param('areaId') areaId: number) {
    const area = await this.sysAreaService.oneByAreaId(areaId);
    return DataObj.create(area);
  }

  /* 更新 */
  @Put()
  @RepeatSubmit()
  @RequiresPermissions('system:area:edit')
  @Log({
    title: '地区管理',
    businessType: BusinessTypeEnum.update,
  })
  async update(@Body(UpdateMessagePipe) updateSysAreaDto: UpdateSysAreaDto) {
    await this.sysAreaService.update(updateSysAreaDto);
  }

  /* 删除 */
  @Delete(':areaIds')
  @RequiresPermissions('system:area:remove')
  @Log({
    title: '地区管理',
    businessType: BusinessTypeEnum.delete,
  })
  async delete(@Param('areaIds') areaId: number) {
    await this.sysAreaService.delete(areaId);
  }
}